home *** CD-ROM | disk | FTP | other *** search
/ Aminet 15 / Aminet 15 - Nov 1996.iso / Aminet / dev / misc / libx11.lha / libX11 / x11colormaps.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-09-22  |  21.1 KB  |  780 lines

  1. /* Copyright (c) 1996 by Terje Pedersen.  All Rights Reserved   */
  2. /*                                                              */
  3. /* By using this code you will agree to these terms:            */
  4. /*                                                              */
  5. /* 1. You may not use this code for profit in any way or form   */
  6. /*    unless an agreement with the author has been reached.     */
  7. /*                                                              */
  8. /* 2. The author is not responsible for any damages caused by   */
  9. /*    the use of this code.                                     */
  10. /*                                                              */
  11. /* 3. All modifications are to be released to the public.       */
  12. /*                                                              */
  13. /* Thats it! Have fun!                                          */
  14. /* TP                                                           */
  15. /*                                                              */
  16.  
  17. /***
  18.    NAME
  19.      colormaps
  20.    PURPOSE
  21.      add colormap handling to libX11
  22.    NOTES
  23.      
  24.    HISTORY
  25.      Terje Pedersen - Oct 22, 1994: Created.
  26. ***/
  27.  
  28. #include <intuition/intuition.h>
  29. #include <intuition/intuitionbase.h>
  30. #include <proto/exec.h>
  31. #include <proto/intuition.h>
  32. #include <proto/graphics.h>
  33. #include <dos.h>
  34. #include <string.h>
  35. #include <stdio.h>
  36. #include <ctype.h>
  37.  
  38. #include "libX11.h"
  39. #define XLIB_ILLEGAL_ACCESS 1
  40.  
  41. #include <X11/X.h>
  42. #include <X11/Xlib.h>
  43. #include <X11/Xutil.h>
  44.  
  45. #include "amigax_proto.h"
  46. #include "amiga_x.h"
  47. #undef memset
  48.  
  49. void savewbcm(void);
  50. void swapwbcm(int cm,ULONG *colors);
  51.  
  52. /* colormaps */
  53.  
  54. extern Display amigaX_display;
  55. extern struct Screen *Scr,*wb;
  56. ULONG AmigaCmap[256*3+2];
  57. ULONG *BackupCmap=NULL;
  58.  
  59. typedef struct {
  60.   unsigned char red;
  61.   unsigned char green;
  62.   unsigned char blue;
  63.   unsigned char pixel;
  64. } X11color_t;
  65.  
  66. typedef struct {
  67.   int nMaxColors;
  68.   X11color_t aColorDef[256];
  69.   unsigned char aAllocMap[256];
  70.   short nAllocNext;
  71.   short nAllocateMax;
  72. } X11ColorMap_t;
  73.  
  74. int prevcm=-1;
  75. int colorno=0;
  76.  
  77. XColor *Backupcolors=NULL;
  78. XColor *Maincolors=NULL;
  79.  
  80. int lockcol=0,lockhigh=0;
  81. char *Xallocated=NULL;
  82. unsigned char *XCanAllocate=NULL,nGlobalAllocateMax;
  83. extern int usewb,wbapp;
  84. extern Screen amiga_screen[];
  85.  
  86. /* *** */
  87. int X11prealloced;
  88.  
  89. /*
  90. XID *X11Cmaps=NULL;
  91. int X11NumCmaps=0,X11AvailCmaps=0;
  92. */
  93.  
  94. void X11init_cmaps(void){
  95. /*  X11Cmaps=NewPointerMap(10,NULL,0);
  96.   X11AvailCmaps=10;*/
  97.   if (Xallocated) return;
  98.   memset(AmigaCmap,0,256*3+2);
  99.   X11prealloced=0;
  100.   if(usewb||wbapp){
  101.     int i;
  102.     Xallocated=(char*)calloc(1,1<<DG.nDisplayDepth);
  103.     BackupCmap=(ULONG*)malloc(sizeof(ULONG)*(256*3+2));
  104.     Backupcolors=(XColor*)malloc(256*sizeof(XColor));
  105.     Maincolors=(XColor*)malloc(256*sizeof(XColor));
  106.     if(!Backupcolors||!Maincolors||!BackupCmap||!Xallocated) X11resource_exit(COLORMAPS2);
  107.     XCanAllocate=(char*)calloc(1,256);
  108.     nGlobalAllocateMax=0;
  109.     for(i=0;i<256;i++){
  110.       int n;
  111.       if((n=ObtainPen(Scr->ViewPort.ColorMap,-1,0,0,0,PEN_EXCLUSIVE|PEN_NO_SETCOLOR))!=-1){
  112.     XCanAllocate[nGlobalAllocateMax++]=n;
  113.       }
  114.     }
  115.     for(i=0;i<256;i++){
  116.       if(XCanAllocate[i])
  117.     ReleasePen(Scr->ViewPort.ColorMap,XCanAllocate[i]);
  118.     }
  119. /*    nCanAllocatePos=2;*/
  120.   }
  121.  
  122. }
  123.  
  124. void X11exit_cmaps(void){
  125.   int i;
  126.   if (!Xallocated) return;
  127.   if(usewb||wbapp){
  128.     swapwbcm(0,NULL);
  129.     for(i=0;i<DG.nDisplayColors;i++)
  130.       if(Xallocated[i]) {
  131.     ReleasePen((struct ColorMap*)amiga_screen[0].cmap,i);
  132.       }
  133.   }
  134.   if(Xallocated) free(Xallocated);
  135.   if(XCanAllocate) free(XCanAllocate);
  136.   Xallocated=NULL;
  137.   if(BackupCmap) free(BackupCmap);
  138.   if(Backupcolors) free(Backupcolors);
  139.   if(Maincolors) free(Maincolors);
  140. }
  141.  
  142. void X11updatecmap(void);
  143.  
  144. void X11updatecmap(void){
  145.   int i;
  146.   if(X11prealloced){
  147.     AmigaCmap[1]=0; AmigaCmap[2]=0; AmigaCmap[3]=0;
  148.     AmigaCmap[4]=255; AmigaCmap[5]=255; AmigaCmap[6]=255;
  149.     for(i=0;i<(1<<DG.nDisplayDepth);i++){
  150.       XColor cdef;
  151.       cdef.pixel=i;
  152.       cdef.red=(unsigned short)AmigaCmap[i*3+1]<<8;
  153.       cdef.green=(unsigned short)AmigaCmap[i*3+2]<<8;
  154.       cdef.blue=(unsigned short)AmigaCmap[i*3+3]<<8;
  155.       XStoreColor(NULL,(Colormap)Scr->ViewPort.ColorMap,&cdef);
  156.     }
  157.   }
  158. }
  159.  
  160. #define SCALE8TO32(x) ((x)|((x)<<8)|((x)<<16)|((x)<<24))
  161.  
  162. XStoreColor(dp,cm,cdef)
  163. Display *dp;
  164. Colormap cm;
  165. XColor *cdef;
  166. {
  167.   int r=cdef->red>>8,g=cdef->green>>8,b=cdef->blue>>8;
  168. #ifdef DEBUGXEMUL_ENTRY
  169.   printf("(colormaps)XStoreColor %d pixel %d\n",cm,cdef->pixel);
  170. #endif
  171.   if(cm!=amiga_screen[0].cmap){ /* not workbench */
  172.     X11ColorMap_t* cmap=(X11ColorMap_t*)cm;
  173.  
  174.     assert(cdef->pixel>=0 && cdef->pixel<256);
  175.     cmap->aColorDef[cdef->pixel].red=cdef->red>>8;
  176.     cmap->aColorDef[cdef->pixel].green=cdef->green>>8;
  177.     cmap->aColorDef[cdef->pixel].blue=cdef->blue>>8;
  178.     cmap->aColorDef[cdef->pixel].pixel=cdef->pixel;
  179.  
  180.     return;
  181.   }
  182.   if((usewb||wbapp)&&cdef->pixel>=DG.nDisplayColors) return;
  183.  
  184.   if(!Scr) return;
  185.   if(cdef->pixel>=0&&cdef->pixel<256){
  186.     if(DG.bUse30){
  187.       SetRGB32(((struct ColorMap*)cm)->cm_vp /*&Scr->ViewPort*/,
  188.            (ULONG)cdef->pixel,
  189.            (ULONG)SCALE8TO32(r),(ULONG)SCALE8TO32(g),(ULONG)SCALE8TO32(b));
  190.     } else
  191.       SetRGB4(&Scr->ViewPort,cdef->pixel,(cdef->red)>>12,(cdef->green)>>12,(cdef->blue)>>12);
  192.   }
  193. }
  194.  
  195. XStoreColors(display, colormap, color, ncolors)
  196.      Display *display;
  197.      Colormap colormap;
  198.      XColor *color;
  199.      int ncolors;
  200. {
  201.   int i;
  202. #ifdef DEBUGXEMUL_ENTRY
  203.   printf("(colormaps)XStoreColors [%d] wb %d\n",ncolors,DG.nDisplayColors);
  204. #endif
  205.   if(colormap!=amiga_screen[0].cmap){ /* not workbench */
  206.     for(i=0;i<ncolors;i++){
  207.       X11ColorMap_t* cmap=(X11ColorMap_t*)colormap;
  208.       cmap->aColorDef[i].red=color[i].red>>8;
  209.       cmap->aColorDef[i].green=color[i].green>>8;
  210.       cmap->aColorDef[i].blue=color[i].blue>>8;
  211.       cmap->aColorDef[i].pixel=color[i].pixel;
  212.     }
  213.     XStoreColors(display,amiga_screen[0].cmap,color,ncolors);
  214.     return(0);
  215.   }
  216.   if(DG.bUse30){
  217.     AmigaCmap[0]=CellsOfScreen(DefaultScreenOfDisplay(display))<<16+0;
  218.     
  219.     for(i=0;i<DG.nDisplayColors;i++){
  220.       AmigaCmap[i*3+1]=SCALE8TO32(BackupCmap[i*3+1]);
  221.       AmigaCmap[i*3+2]=SCALE8TO32(BackupCmap[i*3+2]);
  222.       AmigaCmap[i*3+3]=SCALE8TO32(BackupCmap[i*3+3]);
  223.     }
  224.     for(i=0;i<ncolors;i++){
  225.       AmigaCmap[color[i].pixel*3+1]=SCALE8TO32(color[i].red);
  226.       AmigaCmap[color[i].pixel*3+2]=SCALE8TO32(color[i].green);
  227.       AmigaCmap[color[i].pixel*3+3]=SCALE8TO32(color[i].blue);
  228.     }
  229.     AmigaCmap[CellsOfScreen(DefaultScreenOfDisplay(display))*3+1] = 0L;
  230.     if(Scr)LoadRGB32(&Scr->ViewPort,(ULONG *)AmigaCmap);
  231.   }else{
  232.     UWORD Acolors[256];
  233.     for(i=0;i<(1<<DG.nDisplayDepth);i++){
  234.       Acolors[color[i].pixel]=(UWORD)((((UBYTE)(color[i].red>>12)))<<8|
  235.                       (((UBYTE)(color[i].green>>12)))<<4|
  236.                       ((UBYTE)((color[i].blue>>12))));
  237.     }
  238.     if(Scr)LoadRGB4(&Scr->ViewPort,(UWORD *)Acolors,(1<<DG.nDisplayDepth));
  239.   }
  240.   prevcm=1;
  241.   return(0);
  242. }
  243.  
  244. XQueryColors(display, colormap, defs_in_out, ncolors)
  245.      Display *display;
  246.      Colormap colormap;
  247.      XColor *defs_in_out;
  248.      int ncolors;
  249. {
  250.   int i;
  251. #ifdef DEBUGXEMUL_ENTRY
  252.   printf("(colormaps)XQueryColors [%d]\n",ncolors);
  253. #endif
  254.   if(colormap!=amiga_screen[0].cmap){ /* not workbench */
  255.     for(i=0;i<ncolors;i++){
  256.       X11ColorMap_t* cmap=(X11ColorMap_t*)colormap;
  257.       defs_in_out[i].pixel=cmap->aColorDef[i].pixel;
  258.       defs_in_out[i].red  =cmap->aColorDef[i].red<<8|cmap->aColorDef[i].red;
  259.       defs_in_out[i].green=cmap->aColorDef[i].green<<8|cmap->aColorDef[i].green;
  260.       defs_in_out[i].blue =cmap->aColorDef[i].blue<<8|cmap->aColorDef[i].blue;
  261.     }
  262.     return(0);
  263.   }
  264.   if(DG.bUse30){
  265.     if(Scr)GetRGB32(Scr->ViewPort.ColorMap,0L,(ULONG)256,(ULONG*)&AmigaCmap[1]);
  266.     for(i=0;i<ncolors;i++){
  267.       int c=defs_in_out[i].pixel;
  268.       defs_in_out[i].red  =AmigaCmap[c*3+1]>>16|AmigaCmap[c*3+1]>>24;
  269.       defs_in_out[i].green=AmigaCmap[c*3+2]>>16|AmigaCmap[c*3+2]>>24;
  270.       defs_in_out[i].blue =AmigaCmap[c*3+3]>>16|AmigaCmap[c*3+3]>>24;
  271.     }
  272.   }else{
  273.     ULONG v=0;
  274.     for(i=0;i<ncolors;i++){
  275.       if(Scr)v=GetRGB4(Scr->ViewPort.ColorMap,i);
  276.       if(v!=-1){
  277.     defs_in_out[i].pixel=i;
  278.     defs_in_out[i].blue=((v&15)<<4)<<8;
  279.     defs_in_out[i].green=(((v&(15<<4))>>4)<<4)<<8;
  280.     defs_in_out[i].red=(((v&(15<<8))>>8)<<4)<<8;
  281.       }
  282.     }
  283.   }
  284.   return(0);
  285. }
  286.  
  287. void savewbcm(void){
  288. #ifdef DEBUGXEMUL_ENTRY
  289.   printf("savewbcm! %d %d (%d)\n",prevcm,DG.nDisplayColors,usewb);
  290. #endif
  291.   if(prevcm==1)return;
  292.   if(usewb||wbapp){
  293.     if(DG.bUse30){
  294.       if(Scr)GetRGB32(Scr->ViewPort.ColorMap,0L,(ULONG)DG.nDisplayColors,(ULONG*)&(BackupCmap[1]));
  295.       BackupCmap[0]=(DG.nDisplayColors<<16+0);
  296.       BackupCmap[(DG.nDisplayColors-lockhigh)*3+1]=0;
  297. /*    for(i=0;i<DG.nDisplayColors;i++)
  298.     printf("saved %d (%d %d %d)\n",i,
  299.            BackupCmap[i*3+1]&255,BackupCmap[i*3+2]&255,BackupCmap[i*3+3]&255);*/
  300.     }else{
  301.       if(Scr)
  302.     XQueryColors(&amigaX_display,(Colormap)Scr->ViewPort.ColorMap,(XColor*)Backupcolors,(int)(DG.nDisplayColors-lockhigh));
  303.     }
  304.     DG.bWbSaved=1;
  305.   }
  306. }
  307.  
  308. void swapwbcm(int cm,ULONG *colors){
  309. #ifdef DEBUGXEMUL_ENTRY
  310.   printf("swapwbcm! %d prev %d\n",cm,prevcm);
  311. #endif
  312.   if(usewb){
  313.     if(prevcm!=cm){
  314.       if(cm==1){
  315.     if(DG.bUse30) LoadRGB32(&Scr->ViewPort,(ULONG*)colors);
  316.     else XStoreColors(&amigaX_display,(Colormap)Scr->ViewPort.ColorMap,Maincolors,1<<DG.nDisplayDepth);
  317.       }
  318.       else{
  319. /*    for(i=0;i<DG.nDisplayColors;i++)
  320.       printf("restoring %d (%d %d %d)\n",i,
  321.          BackupCmap[i*3+1]&255,BackupCmap[i*3+2]&255,BackupCmap[i*3+3]&255);*/
  322.     if(DG.bUse30) LoadRGB32(&Scr->ViewPort,(ULONG *)BackupCmap);
  323.     else XStoreColors(&amigaX_display,(Colormap)Scr->ViewPort.ColorMap,Backupcolors,DG.nDisplayColors);
  324.       }
  325.       prevcm=cm;
  326.     }
  327.   }
  328. }
  329.  
  330. XAllocNamedColor(display,colormap,color_name,screen_def_return,exact_def_return)
  331.      Display *display;
  332.      Colormap colormap;
  333.      char *color_name;
  334.      XColor *screen_def_return;
  335.      XColor *exact_def_return;
  336. {/*        File 'sunclock.o'*/
  337.   int ok=XLookupColor(display,colormap,color_name,exact_def_return,screen_def_return);
  338.   if(ok){
  339.     screen_def_return->red=exact_def_return->red;
  340.     screen_def_return->green=exact_def_return->green;
  341.     screen_def_return->blue=exact_def_return->blue;
  342.     ok=XAllocColor(display,colormap,screen_def_return);
  343.     if(!ok) screen_def_return->pixel=colorno++;
  344.     exact_def_return->pixel=screen_def_return->pixel;
  345.     return 1;
  346.   }
  347.   return 0;
  348. /*
  349.   if(stricmp(color_name,"Black")==0){
  350.     screen_def_return->pixel=BlackPixel(display,DefaultScreen(display));
  351.   } else if(stricmp(color_name,"White")==0){
  352.     screen_def_return->pixel=WhitePixel(display,DefaultScreen(display));
  353.   } else{
  354.     screen_def_return->pixel=unused_col++;
  355.   }
  356.   exact_def_return->pixel=screen_def_return->pixel;
  357.  
  358. #ifdef DEBUGXEMUL_ENTRY
  359.   printf("XAllocNamedColor [%s] using %d\n",color_name,screen_def_return->pixel);
  360. #endif
  361.   return(1);
  362. */
  363. }
  364.  
  365. Status XAllocColorCells(display, colormap, contig, plane_masks_return,
  366.             nplanes, pixels_return, npixels_return)
  367.      Display *display;
  368.      Colormap colormap;
  369.      Bool contig;
  370.      unsigned long *plane_masks_return;
  371.      unsigned int nplanes;
  372.      unsigned long *pixels_return;
  373.      unsigned int npixels_return;
  374. {/*        File 'colors.o'*/
  375.   int i,n;
  376. #ifdef DEBUGXEMUL_ENTRY
  377.   printf("XAllocColorCells used %d\n",colorno);
  378. #endif
  379.   if(colormap!=amiga_screen[0].cmap){ /* not workbench */
  380.     for(i=0;i<npixels_return;i++)
  381.       pixels_return[i]=i;
  382.     return(1);
  383.   }
  384.   if((usewb||wbapp)&&DG.bUse30){
  385.     if(!colormap) return(0);
  386.     for(i=0;i<npixels_return;i++){
  387.       if((n=ObtainPen((struct ColorMap*)colormap,-1,0,0,0,PEN_EXCLUSIVE|PEN_NO_SETCOLOR))!=-1){
  388.     pixels_return[i]=n;
  389.     Xallocated[n]=1;
  390.       }
  391.       else return(0);
  392.     }
  393.   }else{
  394.     for(i=0;i<npixels_return;i++){
  395.       if(colorno<(1<<DG.nDisplayDepth)) pixels_return[i]=colorno++;
  396.       else {return(0);}
  397.     }
  398.   }
  399.  
  400.   return(1);
  401. }
  402.  
  403. Status XParseColor(display, colormap, spec, exact_def_return)
  404.      Display *display;
  405.      Colormap colormap;
  406.      char *spec;
  407.      XColor *exact_def_return;
  408. {
  409. #ifdef DEBUGXEMUL_ENTRY
  410.   printf("XParseColor\n");
  411. #endif
  412.   if(XLookupColor(display,colormap,spec,exact_def_return,exact_def_return)){
  413.     if(colormap!=amiga_screen[0].cmap){
  414. /*      exact_def_return->pixel=0;*/
  415.     }else if((usewb||wbapp)&&DG.bUse30){
  416.       ULONG red,green,blue;
  417.       int n;
  418.       red=SCALE8TO32(exact_def_return->red>>8);
  419.       green=SCALE8TO32(exact_def_return->green>>8);
  420.       blue=SCALE8TO32(exact_def_return->blue>>8);
  421.       n=ObtainBestPen((struct ColorMap*)colormap,red,green,blue,NULL);
  422.       Xallocated[n]=1;
  423.       exact_def_return->pixel=n;
  424.     }else{
  425.       exact_def_return->pixel=colorno++;
  426.     }
  427.   }
  428.   return(1);
  429. }
  430.  
  431. XInstallColormap(display, colormap_return)
  432.      Display *display;
  433.      Colormap colormap_return;
  434. {
  435. #if (DEBUGXEMUL_ENTRY) || (DEBUGXEMUL_WARNING)
  436.   printf("WARNING: XInstallColormap no %d\n",colormap_return);
  437. #endif
  438.   return(0);
  439. }
  440.  
  441. XSetWindowColormap(display, w, colormap)
  442.      Display *display;
  443.      Window w;
  444.      Colormap colormap;
  445. {
  446. #ifdef DEBUGXEMUL_ENTRY
  447.   printf("XSetWindowColormap %d\n",w);
  448. #endif
  449.   if(Scr!=NULL&&colormap!=(Colormap)Scr->ViewPort.ColorMap){
  450.     int i;
  451.     X11ColorMap_t *cmap=(X11ColorMap_t*)colormap;
  452.     amiga_screen[0].cmap=(Colormap)Scr->ViewPort.ColorMap;
  453. /*
  454.     for(i=0;i<256;i++){
  455.       Maincolors[i].red=cmap->aColorDef[i].red;
  456.       Maincolors[i].green=cmap->aColorDef[i].green;
  457.       Maincolors[i].blue=cmap->aColorDef[i].blue;
  458.       Maincolors[i].pixel=cmap->aColorDef[i].pixel;
  459.     }
  460.     XStoreColors(display,(Colormap)Scr->ViewPort.ColorMap,Maincolors,256);
  461. */
  462.     for(i=0;i<256;i++){
  463.       XColor cdef;
  464.       cdef.red=cmap->aColorDef[i].red;
  465.       cdef.green=cmap->aColorDef[i].green;
  466.       cdef.blue=cmap->aColorDef[i].blue;
  467.       cdef.pixel=cmap->aColorDef[i].pixel;
  468.       if(cdef.pixel)
  469.     XStoreColor(NULL,(Colormap)Scr->ViewPort.ColorMap,&cdef);
  470.     }
  471.   }
  472.   return(0);
  473. }
  474.  
  475. Colormap XCreateColormap(display, w, visual, alloc)
  476.      Display *display;
  477.      Window w;
  478.      Visual *visual;
  479.      int alloc;
  480. {
  481.   X11ColorMap_t *cm;
  482.   int i;
  483. #ifdef DEBUGXEMUL_ENTRY
  484.   printf("XCreateColormap [%d]\n",alloc);
  485. #endif
  486.  
  487.   alloc=256; /* assume 8 planes, should be available in visual */
  488.  
  489.   if(!(cm=(X11ColorMap_t*)malloc(sizeof(X11ColorMap_t)))) X11resource_exit(COLORMAPS3);
  490. /*  return(GetColorMap(256));*/
  491.   List_AddEntry(pMemoryList,(void*)cm);
  492.  
  493.   cm->nAllocNext=0;
  494.   cm->nAllocateMax=0;
  495.   memset(cm->aAllocMap,0,256);
  496.  
  497.   for(i=0;i<256;i++){
  498.     int n;
  499.     if((n=ObtainPen(Scr->ViewPort.ColorMap,-1,0,0,0,PEN_EXCLUSIVE|PEN_NO_SETCOLOR))!=-1){
  500.       cm->aAllocMap[cm->nAllocateMax++]=n;
  501.     }
  502.   }
  503.   for(i=0;i<256;i++){
  504.     if(cm->aAllocMap[i])
  505.       ReleasePen(Scr->ViewPort.ColorMap,cm->aAllocMap[i]);
  506.   }
  507.  
  508.   return((Colormap)cm);
  509. }
  510.  
  511. XFreeColormap(display, colormap)
  512.      Display *display;
  513.      Colormap colormap;
  514. {/*           File 'xvmisc.o' */
  515. #ifdef DEBUGXEMUL_ENTRY
  516.   printf("XFreeColormap\n");
  517. #endif
  518.   List_RemoveEntry(pMemoryList,(void*)colormap);
  519.   return(0);
  520. }
  521.  
  522. XFreeColors(display, colormap, pixels, npixels, planes)
  523.      Display *display;
  524.      Colormap colormap;
  525.      unsigned long pixels[];
  526.      int npixels;
  527.      unsigned long planes;
  528. {/*             File 'xvcolor.o' */
  529.   int i;
  530.   X11ColorMap_t* cmap=(X11ColorMap_t*)colormap;
  531. #ifdef DEBUGXEMUL_ENTRY
  532.   printf("XFreeColors still allocated[%d]\n",colorno);
  533. #endif
  534.  
  535.   if(colormap!=amiga_screen[0].cmap){ /* not workbench */
  536.     if(npixels==256){
  537.       amiga_screen[1].max_maps=1;
  538.       cmap->nAllocNext=0;
  539.     }
  540.     else amiga_screen[1].max_maps-=npixels;
  541.     return(0);
  542.   }
  543.   if(npixels>DG.nDisplayColors)npixels=DG.nDisplayColors;
  544.   if((usewb||wbapp)&&DG.bUse30){
  545.     for(i=0;i<npixels;i++)
  546.       if(Xallocated[pixels[i]]){
  547.     ReleasePen((struct ColorMap*)colormap,pixels[i]);
  548.     Xallocated[pixels[i]]=0;
  549.       }
  550.   }else{
  551.     if(colorno>0)
  552.       colorno-=npixels;
  553.   }
  554.   return(0);
  555. }
  556.  
  557. hextoint(char c){
  558.   if(c>'9') return toupper(c)-'A'+10;
  559.   else return c-'0';
  560. }
  561.  
  562. int hexscan(char *pStr,int nChars){
  563.   int nSum=0;
  564.   int i;
  565.   for(i=0;i<nChars;i++)
  566.     nSum+=hextoint(*(pStr+(nChars-i-1)))<<i*4;
  567.   return nSum;
  568. }
  569.  
  570. Status XLookupColor(display, colormap, colorname, exact_def_return,
  571.             screen_def_return)
  572.      Display *display;
  573.      Colormap colormap;
  574.      char *colorname;
  575.      XColor *exact_def_return, *screen_def_return;
  576. {/*            File 'xvimage.o' */
  577.   FILE *fp;
  578.   char str[80];
  579.   int r,g,b;
  580. #ifdef DEBUGXEMUL_ENTRY
  581.   printf("XLookupColor\n");
  582. #endif
  583.   if(!colorname) return(0);
  584.   if(colorname[0]=='#'){ /* #FFFF FFFF FFFF */
  585.     int hexeach=4;
  586.     
  587.     if(strlen(colorname)==7)
  588.       hexeach=2;
  589.  
  590.     exact_def_return->red=hexscan(&colorname[1],hexeach);
  591.     exact_def_return->red=exact_def_return->red<<8|exact_def_return->red;
  592.     exact_def_return->green=hexscan(&colorname[1+hexeach],hexeach);
  593.     exact_def_return->green=exact_def_return->green<<8|exact_def_return->green;
  594.     exact_def_return->blue=hexscan(&colorname[1+2*hexeach],hexeach);
  595.     exact_def_return->blue=exact_def_return->blue<<8|exact_def_return->blue;
  596.     return 1;
  597.   }
  598.   if(!(fp=fopen("t:rgb.txt","r"))){
  599.     system("c:copy libx11:rgb.txt t:");
  600.     fp=fopen("t:rgb.txt","r");
  601.   }
  602.   if(!fp) return(0);
  603.   while(!feof(fp)){
  604.     char c;
  605.     fscanf(fp,"%d %d %d%c%c\n",&r,&g,&b,&c,&c);
  606.     fgets(str,80,fp);
  607.     str[strlen(str)-1]=0;
  608. /*    printf("%d %d %d [%s]\n",r,g,b,str);*/
  609.     if(strcmp(str,colorname)==NULL){
  610.       exact_def_return->red=(r<<8)|r;
  611.       exact_def_return->green=(g<<8)|g;
  612.       exact_def_return->blue=(b<<8)|b;
  613.       fclose(fp);
  614.       return(1);
  615.       break;
  616.     }
  617.   }
  618.   fclose(fp);
  619.   return(0);
  620. }
  621.  
  622. XUninstallColormap(display, colormap)
  623.      Display *display;
  624.      Colormap colormap;
  625. {/*      File 'xvevent.o' */
  626. #if (DEBUGXEMUL_ENTRY) || (DEBUGXEMUL_WARNING)
  627.   printf("WARNING: XUninstallColormap\n");
  628. #endif
  629.   return(0);
  630. }
  631.  
  632. Status XAllocColor(display, colormap, cio)
  633.      Display *display;
  634.      Colormap colormap;
  635.      XColor *cio;
  636. {
  637.   int n;
  638.   X11ColorMap_t* cmap=(X11ColorMap_t*)colormap;
  639. #ifdef DEBUGXEMUL_ENTRY
  640.   printf("XAllocColor\n");
  641. #endif
  642.  
  643.   if(colormap==amiga_screen[0].cmap&&(usewb||wbapp||wb==Scr)&&DG.bUse30){
  644.     ULONG red,green,blue;
  645.     red=SCALE8TO32(cio->red>>8);
  646.     green=SCALE8TO32(cio->green>>8);
  647.     blue=SCALE8TO32(cio->blue>>8);
  648.  
  649.     if ((n=ObtainPen((struct ColorMap*)colormap,-1,red,green,blue,PEN_EXCLUSIVE))!=-1){
  650.       cio->pixel=n;
  651.       XStoreColor(display,colormap,cio);
  652.       Xallocated[n]=1;
  653.       return(n);
  654.     }else
  655.       if ((n=ObtainBestPen((struct ColorMap*)colormap,red,green,blue,NULL))!=-1){
  656.     cio->pixel=n;
  657.     Xallocated[n]=1;
  658.     return n;
  659.       }
  660.     return(0);
  661.   }
  662.   if(cio->red==0&&cio->green==0&&cio->blue==0){
  663.     cio->pixel=1;
  664.     
  665.     if(usewb) XStoreColor(display,colormap,cio);
  666.     else cio->pixel=1; /* assume color 1 is black */
  667.     return(1);
  668.   }
  669.   if(cio->red>>8==0xff&&cio->green>>8==0xff&&cio->blue>>8==0xff){
  670.     cio->pixel=2;
  671.     if(usewb) XStoreColor(display,colormap,cio);
  672.     else cio->pixel=2; /* assume color 2 is white */
  673.     return(1);
  674.   }
  675.   if(colormap!=amiga_screen[0].cmap){ /* not workbench */
  676.     cio->pixel=cmap->aAllocMap[cmap->nAllocNext++];
  677.     if(cio->pixel<256 &&cmap->nAllocNext<cmap->nAllocateMax){
  678.       XStoreColor(display,colormap,cio);
  679.       return (int)cio->pixel;
  680.     }
  681.     cio->pixel=0;
  682.     return(0);
  683.   }
  684.   if(!usewb /*wb!=Scr*/){
  685.     cio->pixel=2+colorno++;
  686.     if(cio->pixel>255) return 0;
  687.     if(Scr==NULL){
  688.       X11prealloced=1;
  689.       AmigaCmap[cio->pixel*3+1]=cio->red;
  690.       AmigaCmap[cio->pixel*3+2]=cio->green;
  691.       AmigaCmap[cio->pixel*3+3]=cio->blue;
  692.     }else XStoreColor(display,colormap,cio);
  693.     
  694.     return((int)cio->pixel);
  695.   }
  696.   if(colorno<(1<<DG.nDisplayDepth)){
  697.     cio->pixel=colorno++;
  698.     return((int)cio->pixel);
  699.   }
  700.   return(0);
  701. }
  702.  
  703. Colormap XDefaultColormapOfScreen(Screen *s){/* File 'image_f_io.o'*/
  704. #ifdef DEBUGXEMUL_ENTRY
  705.   printf("XDefaultColormapOfScreen\n");
  706. #endif
  707.   return(s->cmap);
  708. }
  709.  
  710. /* end colormaps */
  711.  
  712. XSetGraphicsExposures(display, gc, graphics_exposures)
  713.      Display *display;
  714.      GC gc;
  715.      Bool graphics_exposures;
  716. {/*   File 'xast.o'*/
  717. #if (DEBUGXEMUL_ENTRY) || (DEBUGXEMUL_WARNING)
  718.   printf("WARNING: XSetGraphicsExposures\n");
  719. #endif
  720.   return(0);
  721. }
  722.  
  723. XStoreNamedColor(Display *display,
  724.          Colormap colormap,
  725.          char *color,
  726.          unsigned long pixel,
  727.          int flags)
  728. {
  729. #if (DEBUGXEMUL_ENTRY) || (DEBUGXEMUL_WARNING)
  730.   printf("WARNING: XStoreNamedColor\n");
  731. #endif
  732.   return(0);
  733. }
  734.  
  735. XStandardColormap *XAllocStandardColormap()
  736. {/*  File 'magick/libMagick.lib' */
  737.   XStandardColormap *xsc=malloc(sizeof(XStandardColormap));
  738. #ifdef DEBUGXEMUL_ENTRY
  739.   printf("XAllocStandardColormap\n");
  740. #endif
  741.   List_AddEntry(pMemoryList,(void*)xsc);
  742.   return(xsc);
  743. }
  744.  
  745. Status XGetRGBColormaps(display, w, std_colormap_return, count_return, property)
  746.      Display *display;
  747.      Window w;
  748.      XStandardColormap **std_colormap_return;
  749.      int *count_return;
  750.      Atom property;
  751. {/*        File 'magick/libMagick.lib' */
  752. #if (DEBUGXEMUL_ENTRY) || (DEBUGXEMUL_WARNING)
  753.   printf("WARNING: XGetRGBColormaps\n");
  754. #endif
  755.   return(0);
  756. }
  757.  
  758. Status XGetWMColormapWindows(display, w, colormap_windows_return, count_return)
  759.      Display *display;
  760.      Window w;
  761.      Window **colormap_windows_return;
  762.      int *count_return;
  763. {/*   File 'magick/libMagick.lib' */
  764. #if (DEBUGXEMUL_ENTRY) || (DEBUGXEMUL_WARNING)
  765.   printf("WARNING: XGetWMColormapWindows\n");
  766. #endif
  767.   return(0);
  768. }
  769.  
  770. Colormap *XListInstalledColormaps(display, w, num_return)
  771.      Display *display;
  772.      Window w;
  773.      int *num_return;
  774. {/* File 'magick/libMagick.lib' */
  775. #if (DEBUGXEMUL_ENTRY) || (DEBUGXEMUL_WARNING)
  776.   printf("WARNING: XListInstalledColormaps\n");
  777. #endif
  778.   return(0);
  779. }
  780.